package com.ts.api.module.channel.controller;

import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.entity.AjaxResult;
import com.ts.api.exp.BizException;
import com.ts.api.module.channel.service.ChannelMarketService;
import com.ts.api.module.visitor.service.ChannelVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 渠道访问控制层
 */
@RestController
@RequestMapping("/channel/visitor")
public class ChannelVisitorController {

    @Autowired
    private ChannelVisitorService channelVisitorService;
    @Autowired
    private ChannelMarketService marketService;

    /**
     * 贷超用户访问量
     *
     * @param marketId 贷超主键id
     * @return AjaxResult
     */
    @PostMapping("/market/{marketId}")
    public AjaxResult marketVisitor(@PathVariable("marketId") Long marketId) {
        if (marketId == null) {
            BizException.fastFail(BizEnum.EMPTY);
        }

        Long channelId = ChannelContext.getId();

        //  检测参数
        int count = marketService.countByChannelIdAndMarketId(channelId, marketId);
        if (count == 0) {
            BizException.fastFail(BizEnum.MARKET_EMPTY);
        }

        channelVisitorService.insertMarketVisitor(marketId);
        return AjaxResult.success();
    }
}
